package com.woniu.car.controller;
import com.woniu.car.annotation.ShopApplyInfoCheck;
import com.woniu.car.annotation.ShopDetailCheck;
import com.woniu.car.common.BeanCopyUtils;
import com.woniu.car.common.ResponseEntity;
import com.woniu.car.domain.TShops;
import com.woniu.car.dto.*;
import com.woniu.car.exception.AccountException;
import com.woniu.car.exception.ParamException;
import com.woniu.car.exception.StatisticsException;
import com.woniu.car.feignclient.ElasticClient;
import com.woniu.car.param.*;
import com.woniu.car.parameter.ElasticShopAddParameter;
import com.woniu.car.service.TExamineService;
import com.woniu.car.service.TShopService;
import com.woniu.car.util.Code;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author WWX
 * @since 2021-06-19
 */
@Api(tags = "商户控制器")
@RestController
@RequestMapping("/tShop")
public class TShopController {
    @Resource
    private TShopService tShopService;
    @Resource
    private TExamineService tExamineService;
    @Resource
    private ElasticClient elasticClient;
    /**
     *@Author 王维鑫
     *@Description 基础信息提交
     *@Param
     *@return
     **/
    @ShopApplyInfoCheck
    @ApiOperation("商户基础信息提交接口")
    @PostMapping("baseInfoCommit")
    public ResponseEntity baseInfoCommit(ShopApplyParam shopApplyParam) throws Exception {
        ShopBaseInfoDto shopBaseInfoDto = tShopService.shopApply(shopApplyParam);
        return ResponseEntity.builder(ShopBaseInfoDto.class).setObject(shopBaseInfoDto).setCode(Code.NORMAL).setMsg("基础信息保存成功");
    }
    /**
     *@Author 王维鑫
     *@Description 资质文件上传
     *@Param
     *@return
     **/
    @ApiOperation("商户资质文件提交接口")
    @PostMapping(value = "qualificationPhotoUpload",headers = "content-type=multipart/form-data")
    public ResponseEntity qualificationPhotoUpload( @RequestParam(value = "files", required = true) MultipartFile files) throws Exception {
            String s = tShopService.qualificationPhotoUpload(files);
        ShopUploadUrlDto shopUploadUrlDto = new ShopUploadUrlDto();
        shopUploadUrlDto.setUploadUrl(s);
        return ResponseEntity.builder(ShopUploadUrlDto.class).setObject(shopUploadUrlDto).setMsg("文件上传成功").setCode(Code.NORMAL);
    }
    /**
     *@Author 王维鑫
     *@Description 资质文件地址保存
     *@Param
     *@return
     **/
    @ApiOperation("资质文件地址保存")
    @PostMapping("insertShopQualificationUrl")
    public ResponseEntity insertShopQualificationUrl(ShopQualificationUrlParam param) throws Exception {
        ShopUrlDto shopUrlDto = tShopService.saveQualificationUrl(param);
        return ResponseEntity.builder(ShopUrlDto.class).setObject(shopUrlDto).setMsg("保存成功").setCode(Code.NORMAL);
    }

    /**
     *@Author 王维鑫
     *@Description 查看商铺基本信息
     *@Param
     *@return
     **/
    @ApiOperation("查看商铺基本信息")
    @GetMapping("showShopInfo")
    public ResponseEntity showShopInfo(ShowShopInfoParam showShopInfoParam) throws Exception {
        TShops tShops = new TShops();
        tShops.setShopId(showShopInfoParam.getShopId());
        List<TShops> tShops1 = tShopService.queryShop(tShops);
        if(tShops1.size()==0){
            throw new AccountException("未找到该商铺");
        }else {
            TShops tShops2 = tShops1.get(0);
            ShopBaseInfoDto shopBaseInfoDto = new ShopBaseInfoDto();
            BeanCopyUtils.copyProperties(tShops2,shopBaseInfoDto);
            return ResponseEntity.builder(ShopBaseInfoDto.class).setObject(shopBaseInfoDto).setCode(Code.NORMAL).setMsg("查询到商铺基本信息");
        }
    }

    /**
     *@Author 王维鑫
     *@Description 商铺详细信息提交
     *@Param
     *@return
     **/
    @ShopDetailCheck
    @ApiOperation("商铺详细信息提交")
    @PostMapping("detailedInfoCommit")
    public ResponseEntity detailedInfoCommit(ShopDetailedInfoParam param) throws Exception {
        TShops tShops = tShopService.uploadDetailInfo(param);
        //将商铺信息存到ES
        ElasticShopAddParameter elasticShopAddParameter = new ElasticShopAddParameter();
        BeanCopyUtils.copyProperties(param,elasticShopAddParameter);
        elasticClient.ElasticShopAdd(elasticShopAddParameter);
        return new ResponseEntity().setCode(Code.NORMAL).setMsg("信息提交成功");
    }
    /**
     *@Author 王维鑫
     *@Description 查看周边商铺
     *@Param
     *@return
     **/
    @ApiOperation("查看周边商铺")
   // @ShopLocationCheck
    @GetMapping("showAroundShop")
    public ResponseEntity showAroundShop(QueryAroundShopParam param){
        PageDto pageDto = tShopService.queryAroundShop(param);
        return ResponseEntity.builder(PageDto.class).setObject(pageDto).setCode(Code.NORMAL).setMsg("查询到商铺");
    }

    /**
     *@Author 王维鑫
     *@Description 商铺图片提交
     *@Param
     *@return
     **/
    @ApiOperation("商铺图片提交")
    @PostMapping(value = "shopPhotoUpload",headers = "content-type=multipart/form-data")
    public ResponseEntity shopPhotoUpload( @RequestParam(value = "file", required = true) MultipartFile file) throws Exception {
        String s = tShopService.uploadShopImg(file);
        ShopUploadUrlDto shopUploadUrlDto = new ShopUploadUrlDto();
        shopUploadUrlDto.setUploadUrl(s);
        return ResponseEntity.builder(ShopUploadUrlDto.class).setObject(shopUploadUrlDto).setMsg("图片提交成功").setCode(Code.NORMAL);
    }
    /**
     *@Author 王维鑫
     *@Description 商铺图片地址保存
     *@Param
     *@return
     **/
    @ApiOperation("商铺图片地址保存")
    @PostMapping("shopPhotoUrl")
    public ResponseEntity shopPhotoUrl(ShopQualificationUrlParam param) throws ParamException {
        ShopPhotoUrlDto shopPhotoUrlDto = tShopService.shopPhotoUrlSave(param);
        return ResponseEntity.builder(ShopPhotoUrlDto.class).setObject(shopPhotoUrlDto).setMsg("图片地址保存成功");
    }


    /**
     *@Author 王维鑫
     *@Description 查看运营商铺
     *@Param
     *@return
     **/
    @ApiOperation("查看运营商铺")
    @GetMapping("queryPassShop")
    public ResponseEntity queryPassShop() throws ParamException {
        PageDto pageDto = tShopService.queryPassShop();
        return ResponseEntity.builder(PageDto.class).setObject(pageDto).setMsg("查询成功");
    }
    /**
     *@Author 王维鑫
     *@Description 查看冻结商铺
     *@Param
     *@return
     **/
    @ApiOperation("查看冻结商铺")
    @GetMapping("queryForzenShop")
    public ResponseEntity queryForzenShop() throws ParamException {
        PageDto pageDto = tShopService.queryForzenShop();
        return ResponseEntity.builder(PageDto.class).setObject(pageDto).setMsg("查询成功");
    }

    /**
     *@Author 王维鑫
     *@Description 查看封禁商铺
     *@Param
     *@return
     **/
    @ApiOperation("查看封禁商铺")
    @GetMapping("queryBanShop")
    public ResponseEntity queryBanShop() throws ParamException {
        PageDto pageDto = tShopService.queryBanShop();
        return ResponseEntity.builder(PageDto.class).setObject(pageDto).setMsg("查询成功");
    }

    @ApiOperation("封禁商铺")
    @GetMapping("banShop")
    public ResponseEntity banShop(BanShopParam param) throws StatisticsException {
        BanShopDto banShopDto = tShopService.banShop(param);
        return ResponseEntity.builder(BanShopDto.class).setObject(banShopDto).setMsg("删除成功").setCode(Code.NORMAL);
    }


    /**
     *@Author 王维鑫
     *@Description 根据关键字搜索店铺
     *@Param
     *@return
     **/
    @ApiOperation("根据关键字搜索商铺")
    @GetMapping("searchShop")
    public ResponseEntity searchShop(@PathVariable("keyWord") String keyWord) throws StatisticsException {
        ResponseEntity responseEntity = elasticClient.ElasticShopSearch(keyWord);
        return responseEntity;
    }
    /**
     *@Author 王维鑫
     *@Description 修改商铺信息
     *@Param
     *@return
     **/
    @ApiOperation("修改商铺信息")
    @GetMapping("updateShop")
    public ResponseEntity updateShop(UpdateShopParam param) throws StatisticsException {
        ShopInfoDto shopInfoDto = tShopService.updateShop(param);
        return  ResponseEntity.builder(ShopInfoDto.class).setObject(shopInfoDto).setMsg("修改成功").setCode(Code.NORMAL);
    }









}

